package com.atung.merchant.controller;

import com.atung.merchant.result.ApiResult;
import com.atung.merchant.service.MenuService;
import com.atung.merchant.vo.*;
import com.atung.merchant.vo.query.QueryMenuListRequest;
import com.atung.merchant.vo.query.QueryMenuTypeRequest;
import com.atung.merchant.vo.save.SaveMenuRequest;
import com.atung.merchant.vo.save.SaveMenuTypeRequest;
import com.atung.merchant.vo.save.SaveOrderMenuRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/api/menu")
@Api(tags = "菜单管理")
public class MenuController {
    @Resource
    private MenuService menuService;

    @PostMapping("/type/list")
    @ApiOperation("获取菜单分类列表")
    public ApiResult getMealMenuTypeList(@RequestBody QueryMenuTypeRequest queryMenuTypeRequest) {
        List<MealMenuTypeResponse> responseList = menuService.getMealMenuTypeService(queryMenuTypeRequest);
        return ApiResult.ok(responseList);
    }

    @PostMapping("/save")
    @ApiOperation("新增菜品")
    @PreAuthorize("hasRole('MASTER')")
    public ApiResult saveMenu(@RequestBody @Valid SaveMenuRequest saveMenuRequest) {
        try {
            menuService.saveMenuService(saveMenuRequest);
            return ApiResult.ok();
        } catch (Exception e) {
            return ApiResult.fail();
        }

    }

    @PostMapping("/import")
    @ApiOperation("导入菜品")
    @PreAuthorize("hasRole('MASTER')")
    public ApiResult importExcelMenu(@RequestParam("file") MultipartFile file) throws Exception {
        return menuService.importExcelMenuService(file);
    }

    @PostMapping("/delete")
    @ApiOperation("删除菜单")
    @PreAuthorize("hasRole('MASTER')")
    public ApiResult deleteMenu(@RequestBody  @Valid IdRequest idRequest) {
        return menuService.deleteMenuService(idRequest);
    }

    @PostMapping("/save/type")
    @ApiOperation("新增菜单分类")
    @PreAuthorize("hasRole('MASTER')")
    public ApiResult saveMenuType(@RequestBody @Valid SaveMenuTypeRequest saveMenuTypeRequest) {
        try {
            menuService.saveMenuTypeService(saveMenuTypeRequest);
            return ApiResult.ok();
        } catch (Exception e) {
            return ApiResult.fail();
        }

    }

    @PostMapping("/delete/type")
    @ApiOperation("删除菜单分类")
    @PreAuthorize("hasRole('MASTER')")
    public ApiResult deleteMenuType(@RequestBody  @Valid IdRequest idRequest) {
        return menuService.deleteMenuTypeService(idRequest);
    }

    @PostMapping("/order/list")
    @ApiOperation("获取订单菜品信息")
    public ApiResult<MenuInfoListResponse> getOrderMenuList(@RequestBody QueryMenuListRequest request) {
        MenuInfoListResponse menuInfoListResponses = menuService.getOrderMenuListService(request);
        return ApiResult.ok(menuInfoListResponses);
    }

    @PostMapping("/order/details/list")
    @ApiOperation("获取订单菜品信息")
    public ApiResult<List<MenuInfoResponse>> getMealOrderMenuDetailsList(@RequestBody QueryMenuListRequest request) {
        List<MenuInfoResponse> menuInfoListResponses = menuService.getMealOrderMenuDetailsList(request);
        return ApiResult.ok(menuInfoListResponses);
    }


    @PostMapping("/order")
    @ApiOperation("选择菜品")
    public ApiResult saveMenuOrder(@RequestBody @Valid SaveOrderMenuRequest request) {
        return menuService.saveMenuOrderService(request);
    }

    @PostMapping("/cancel")
    @ApiOperation("取消菜品")
    public ApiResult cancelMenuOrder(@RequestBody  @Valid IdRequest request) {
        return menuService.cancelMenuOrderService(request);
    }


    @PostMapping("/serving")
    @ApiOperation("出餐")
    public ApiResult servingMenuOrder(@RequestBody  @Valid IdRequest request) {
        return menuService.servingMenuOrderService(request);
    }

    @PostMapping("serving/list")
    @ApiOperation("获取带出餐列表")
    public ApiResult servingMenuOrderList(@RequestBody IdRequest request) {

        List<ServingMenuOrderResponse>servingMenuOrderResponses=menuService.servingMenuOrderListService(request.getId());
        return ApiResult.ok(servingMenuOrderResponses);

    }
    @PostMapping("serving/details")
    @ApiOperation("获取带出餐列表")
    public ApiResult servingMenuOrderDetails(@RequestBody @Valid IdRequest idRequest) {

        ServingMenuOrderResponse servingMenuOrderResponses=menuService.servingMenuOrderDetailsService(idRequest.getId());
        return ApiResult.ok(servingMenuOrderResponses);

    }
}
